package org.csu.equipment.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.csu.equipment.entity.Client;
import org.csu.equipment.entity.Employee;
import org.csu.equipment.entity.Task;
import org.csu.equipment.service.ClientService;
import org.csu.equipment.service.EmployeeService;
import org.csu.equipment.vo.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class RestClientController {

    @Autowired
    ClientService clientService;
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/appClientLogin")
    public String appClientLogin(String CId,String CPwd){
        MD5 md5 = new MD5();
        CPwd = md5.GetMD5Code(CPwd);
        Integer c=clientService.login(CId,CPwd);
        if(c==1){
            return "SUCCESS";
        }
        else return "FAILURE";
    }

    @GetMapping("/appEmployeeLogin")
    public String appEmployeeLogin(String EId,String EPwd){
        MD5 md5 = new MD5();
        EPwd = md5.GetMD5Code(EPwd);
        Integer c=employeeService.login(EId,EPwd);
        if(c==1){
            return "SUCCESS";
        }
        else return "FAILURE";
    }

    @GetMapping("/getEmployeeTaskToSelect")
    public String getEmployeeTaskToSelect(String EId){
        System.out.println("EId="+EId);
        List<Task> taskList=employeeService.getTasksToSelect(EId);
        return JSON.toJSONString(taskList);
     }

     @GetMapping("/getTaskToDo")
    public String getTaskToDo(String TId,String EId,String material){
         return employeeService.getTask(TId,EId,material);
     }

     @GetMapping("/appFinish")
    public String appFinish(String TId,String EId){
         employeeService.finishTask(TId,EId);
         return "SUCCESS";
     }

     @GetMapping("/getATaskCanBeFinish")
    public String getATaskCanBeFinish(String EId){
        Task task = employeeService.getATask(EId);
        return JSON.toJSONString(task);
     }

     @GetMapping("/getMyEmployee")
    public String getMyEmployee(String EId){
         Employee employee = employeeService.getEmployee(EId);
         return JSON.toJSONString(employee);
     }

     @GetMapping("/getMyClient")
    public String getMyClient(String CId){
        Client client = clientService.getClient(CId);
        return JSON.toJSONString(client);
     }

     @GetMapping("/appNewTask")
    public String appNewTask(String CId,String skill,String description){
        Client client = clientService.getClient(CId);
        clientService.newTask(client,description,skill);
        return "SUCCESS";
     }

}
